<?php $this->headScript()->captureStart() ?>
$(document).ready(function() {

    $('li.attachment').find(".delete").livequery('click', function() {
        var currIndex = $('li.attachment').find(".delete").index($(this));
        var currLi = $($('li.attachment').get(currIndex));
        var attachment = currLi.find("input[name='attachment']").val();
        
        dialogConfirm($.languages.areYouSureToDelete, function(){
            $.ajax({   
                type: "post",   
                url: "<?php echo $this->url(array('module'=>'publish', 'controller'=>'attachment', 'action'=>'delete-attachment'),'default',true) ?>", 
                data: { attachment: attachment, articleId : '<?php echo $this->articleId;?>'},
                beforeSend: function() {   
                },   
                success:function(responseText, statusText){
                    var json = jQuery.parseJSON(responseText);
                    if(json.result != 1){
                        dialogMessage(json.messages);
                        return "";
                    }
                    
                    currLi.remove();
                    return true;
                }, 
            });
        });
    });

    $( "#attachments" ).sortable({
        opacity: 0.6,
        revert: true,
        cursor: 'move',
        handle: '.move', 
        update: function(event, ui){
            var newOrder = [];   
            $( "#attachments" ).find("input[name='attachment']").each(function(i, value) {   
                newOrder.push(this.value);   
            });   

            $.ajax({   
                type: "post",   
                url: "<?php echo $this->url(array('module'=>'publish', 'controller'=>'attachment', 'action'=>'save-attachments-order'),'default',true) ?>", 
                data: { newOrder: newOrder, articleId : '<?php echo $this->articleId;?>'},
                beforeSend: function() {
                },   
                success:function(responseText, statusText){
                    var json = jQuery.parseJSON(responseText);
                    if(json.result != 1){
                        dialogMessage(json.messages);
                        return "";
                    }

                    return true;
                }, 
            });
        }
    });

    $('#attachments').imagegallery();
    
    $(".attachment .video").find('a[rel="player"]').livequery("click", function(){
        var $url   = $(this).attr("data-href");
        var $title = $(this).attr("title");

        var player = $('<div id="player" />');
        player.flash({
            src: $url,
            width: 470,
            height: 300,
            allowFullScreen:'true',
        },
        {
            version: 8
        });

        $( "#dialog-player" ).dialog( "destroy" );
        if(!$( "#dialog-player" ).length){  
            $("body").append("<div id='dialog-player' />");  
        } 
        $( "#dialog-player" ).html(player.html());
        $( "#dialog-player" ).dialog({
            bgiframe: true,
            title: $title,  
            modal: true,
            width: 500,
        });
    });

    $(".attachment .music").find('a[rel="player"]').livequery("click", function(){
        var $url   = $(this).attr("data-href");
        var $title = $(this).attr("title");

        var playerData = {flash: $url};
        var player = $('#template-music-player').tmpl(playerData);
        
        $( "#dialog-player" ).dialog( "destroy" );
        if(!$( "#dialog-player" ).length){  
            $("body").append("<div id='dialog-player' />");  
        } 
        $( "#dialog-player" ).empty();
        $( "#dialog-player" ).prepend(player);
        $( "#dialog-player" ).dialog({
            bgiframe: true,
            title: $title,  
            modal: true,
            width: 320,
            height: 160,
        });
    });
    
});
<?php $this->headScript()->captureEnd() ?>

<dt><?php echo $this->translate("Attachment list");?></dt>
<dd>
    <ul id="attachments" class="ui-corner-all">
    <?php if(is_array($this->attachments)):?>
        <?php foreach($this->attachments as $key=>$attachment){?>
        <?php 
            if($attachment->type == 'video')
            {
                echo $this->partial('module_publish_script_show-video.phtml', array(
                    'data'             => $attachment->data,
                ));
            }
            elseif($attachment->type == 'music')
            {
                echo $this->partial('module_publish_script_show-music.phtml', array(
                    'data'             => $attachment->data,
                ));
            }
            elseif($attachment->type == 'image')
            {
                echo $this->partial('module_publish_script_show-image.phtml', array(
                    'data'             => $attachment->data,
                ));
            }
            elseif($attachment->type == 'shopping')
            {
                echo $this->partial('module_publish_script_show-shopping.phtml', array(
                    'data'             => $attachment->data,
                ));
            }
        ?>
        <?php }?>
    <?php endif;?>
    </ul>
</dd>

<script id="template-music-player" type="text/x-jquery-tmpl">
    <div class="box">
        <embed src="${flash}" type="application/x-shockwave-flash" width="257" height="33" wmode="transparent"></embed>
    </div>
</script>